﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace DarwinIsDead {
    public abstract class Weapon : IDisposable {
        protected World World;
        protected Entity Owner;
        protected bool Friendly;
        protected List<Bullet> Bullets;
        protected Matrix SpriteTransform;
        
        public Vector2 Position { get; set; }
        public Texture2D Sprite { get; set; }
        public Vector2 SpriteAxis { get; set; }
        public float BarrelLength { get; set; }
        public float Direction { get; set; }

        private bool flip;

        public Weapon(World world, Texture2D sprite, Vector2 spriteAxis,
            float barrelLength, bool friendly) {
                World = world;
                Sprite = sprite;
                SpriteAxis = spriteAxis;
                BarrelLength = barrelLength;
                Friendly = friendly;
                Direction = 0f;

                Bullets = new List<Bullet>();
                Position = new Vector2();
                
                flip = false;
        }

        public virtual void Dispose() {
            for (int i = 0; i < Bullets.Count; i++) {
                World.RemoveEntity(Bullets[i]);
            }
            Bullets.Clear();
        }

        public void Update(GameTime gameTime) {
            //SpriteTransform = Matrix.CreateFromAxisAngle(new Vector3(SpriteAxis, 0f), Direction);
            int deg = (int)(180f * Direction / Math.PI) % 360;

            if (deg <= 90 && deg > -90) {
                flip = false;
            } else {
                flip = true;
            }
            InternalUpdate(gameTime);
        }

        public virtual void InternalUpdate(GameTime gameTime) {

        }

        public virtual void Draw(SpriteBatch spriteBatch) {
            spriteBatch.Draw(Sprite, Vector2.Add(Position, SpriteAxis), null, Color.White, Direction, Vector2.Zero, 2f,
                flip ? (SpriteEffects.FlipVertically) : SpriteEffects.None, 0f);
        }

        public virtual void DrawUI(SpriteBatch spriteBatch) {

        }
    }
}
